package com.ingenio.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ingenio.business.Almacen;
import com.ingenio.business.ConceptoMovimiento;
import com.ingenio.business.DetalleMovimiento;
import com.ingenio.business.Movimiento;
import com.ingenio.business.Producto;
import com.ingenio.dao.AlmacenAdapter;
import com.ingenio.dao.ConceptoMovimientoAdapter;
import com.ingenio.dao.InventarioAdapter;
import com.ingenio.dao.ProductoAdapter;

/**
 * Servlet implementation class AjusteInventarioServlet
 */
@WebServlet("/AjusteInventario")
public class AjusteInventarioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private List<DetalleMovimiento> detallesMovs;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AjusteInventarioServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		String url = request.getHeader("referer");
//		String[] parts = url.split("/");
		
		String[] id_productos = request.getParameterValues("id_producto");
		String[] cant_productos = request.getParameterValues("cantidadAjustada");
		Integer idConcepto = Integer.parseInt(request.getParameter("conceptoMov"));
		Integer idAlmacen = Integer.parseInt(request.getParameter("almacen"));
		String observ = request.getParameter("obsAjuste");
		
		try {
				detallesMovs = new ArrayList<DetalleMovimiento>();
				ProductoAdapter pa = new ProductoAdapter();
				Producto pr = null;
				AlmacenAdapter aa = new AlmacenAdapter();
				Almacen a;
				a = aa.devolverAlmacen(idAlmacen);
				InventarioAdapter ia = new InventarioAdapter();
				
				ConceptoMovimientoAdapter cma = new ConceptoMovimientoAdapter();
				ConceptoMovimiento cm = cma.devolverConcepto(idConcepto);
				
				DetalleMovimiento dm = null;
				
				for (int i = 0; i < cant_productos.length; i++) {
					if(isDouble(cant_productos[i])){
						String id = id_productos[i];
						Double cant = Double.parseDouble(cant_productos[i]);
						pr = pa.devolverProducto(id);
						dm = new DetalleMovimiento(null, pr, a, cant);
						productoExistente(dm);
					}
				}
				
				Movimiento m = new Movimiento(null, new Date(), cm, null, observ);
				m.setDetallesMov(detallesMovs);
				
				ia.ajusteInventario(m, a);
				
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static boolean isDouble(String str)  
	{  
	  try  
	  {  
	    @SuppressWarnings("unused")
		Double i = Double.parseDouble(str);  
	  }  
	  catch(NumberFormatException nfe)  
	  {  
	    return false;  
	  }  
	  return true;  
	}
	
	public static boolean isInteger(String str)  
	{  
	  try  
	  {  
	    @SuppressWarnings("unused")
		Integer i = Integer.parseInt(str);  
	  }  
	  catch(NumberFormatException nfe)  
	  {  
	    return false;  
	  }  
	  return true;  
	}
	
	public void productoExistente(DetalleMovimiento dm){
		if(detallesMovs.contains(dm)){
			Integer ind = detallesMovs.indexOf(dm);
			DetalleMovimiento dm_aux = detallesMovs.get(ind);
			Double cant = dm_aux.getCantidadMov() + dm.getCantidadMov();
			dm_aux.setCantidadMov(cant);
		}else{
			detallesMovs.add(dm);
		}
	}
}
